package com.rui.controller;

import com.rui.domain.*;
import com.rui.service.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/student")
public class StudentController {

    private static final String LOG_PATH = "D:/UploadFile/internshipLog";
    private static final String NO_READ = "未阅";
    private static final String STUDENT = "student";
    private static final String FLAG = "flag";

    @Autowired
    private DevelopmentLanguageService developmentLanguageService;
    @Autowired
    private DataBaseService dataBaseService;
    @Autowired
    private FrameService frameService;
    @Autowired
    private InternshipService internshipService;
    @Autowired
    private JobService jobService;
    @Autowired
    private InternshipLoggerService internshipLoggerService;
    @Autowired
    private StudentService studentService;

    @RequestMapping("/index")
    public String index() {
        return "student/index";
    }

    @RequestMapping("/toInputInternship.action")
    public String toInputInternship() {
        return "student/inputInternship";
    }

    @RequestMapping(value = "/getDevelopmentLanguageList.action", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getDevelopmentLanguageList() {
        Map<String, Object> map = new HashMap<>();

        //查询开发语言表，获取所有开发语言信息
        List<DevelopmentLanguage> developmentLanguages = developmentLanguageService.findAll();
        map.put("developmentLanguage", developmentLanguages);

        return map;
    }

    @RequestMapping(value = "/getDataBaseList.action", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getDataBaseList() {
        Map<String, Object> map = new HashMap<>();

        //查询数据库表，获取所有数据库信息
        List<DataBase> dataBases = dataBaseService.findAll();
        map.put("dataBases", dataBases);

        return map;
    }

    @RequestMapping(value = "/getFrameList.action", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getFrameList() {
        Map<String, Object> map = new HashMap<>();

        //查询数据库表，获取所有数据库信息
        List<Frame> frames = frameService.findAll();
        map.put("frames", frames);

        return map;
    }

    @RequestMapping(value = "/addInternship.action", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addInternship(Internship internship, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //使用UUID设置id
        internship.setId(UUID.randomUUID().toString());
        //设置用户
        Student student = (Student) session.getAttribute(STUDENT);
        internship.setStudentNumber(student.getStuNo());
        boolean b = internshipService.insertInternship(internship);
        if (b) {
            map.put(FLAG,true);
        }else {
            map.put(FLAG,false);
        }
        return map;
    }

    @RequestMapping("/toShowInternshipPage.action")
    public String toShowInternshipPage() {
        return "student/showInternshipInfo";
    }

    @RequestMapping("/getInternshipInfo.action")
    @ResponseBody
    public Map<String, Object> getInternshipInfo(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //先查询当前学生的实习信息
        Student student = (Student) session.getAttribute("student");
        List<InternshipVO> internshipVOList = internshipService.findInternshipByStudentNumber(student.getStuNo());
        System.out.println(internshipVOList);
        map.put("data",internshipVOList);
        map.put("count",internshipVOList.size());
        map.put("code",0);
        map.put("msg","没有查询到实习信息");
        return map;
    }

    @RequestMapping("/updateInternship.action")
    @ResponseBody
    public Map<String, Object> updateInternship(Internship internship, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Student student = (Student) session.getAttribute("student");
        internship.setStudentNumber(student.getStuNo());
        //调用业务层的internshipService的updateInternshipById方法
        boolean b = internshipService.updateInternshipById(internship);
        if (b) {
            map.put("flag",true);
        }else {
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("/deleteInternship.action")
    @ResponseBody
    public Map<String, Object> deleteInternship(String id) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(id);
        //调用deleteInternshipById方法
        boolean b = internshipService.deleteInternshipById(id);
        if (b) {
            map.put("flag",true);
        }else {
            map.put("flag",false);
        }
        return map;
    }

    @GetMapping("/toInputJobPage.action")
    public String toInputJobPage() {
        return "student/inputJob";
    }

    @RequestMapping("/addJob.action")
    @ResponseBody
    public Map<String, Object> addJob(Job job, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //使用UUID设置id
        job.setId(UUID.randomUUID().toString());
        //设置用户
        Student student = (Student) session.getAttribute("student");
        job.setStudentNumber(student.getStuNo());
        boolean b = jobService.insertJob(job);
        if (b) {
            map.put("flag",true);
        }else {
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("/toShowJobPage.action")
    public String toShowJobPage() {
        return "student/showJobInfo";
    }

    @RequestMapping("/getJobInfo.action")
    @ResponseBody
    public Map<String, Object> getJobInfo(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //先查询当前学生的实习信息
        Student student = (Student) session.getAttribute("student");
        List<JobVO> jobVOList = jobService.findJobByStudentNumber(student.getStuNo());
        map.put("data",jobVOList);
        map.put("count",jobVOList.size());
        map.put("code",0);
        map.put("msg","没有查询到实习信息");
        return map;
    }

    @RequestMapping("/updateJob.action")
    @ResponseBody
    public Map<String, Object> updateJob(Job job, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Student student = (Student) session.getAttribute("student");
        job.setStudentNumber(student.getStuNo());
        //调用业务层的internshipService的updateInternshipById方法
        boolean b = jobService.updateJobById(job);
        if (b) {
            map.put("flag",true);
        }else {
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("/deleteJob.action")
    @ResponseBody
    public Map<String, Object> deleteJob(String id) {
        Map<String, Object> map = new HashMap<>();
        //System.out.println(id);
        //调用deleteInternshipById方法
        boolean b = jobService.deleteJobById(id);
        if (b) {
            map.put("flag",true);
        }else {
            map.put("flag",false);
        }
        return map;
    }

    @GetMapping("/toEditInternshipLog.action")
    public String toEditInternshipLog() {
        return "student/editInternshipLog";
    }

    @RequestMapping("/uploadInternshipLog.action")
    @ResponseBody
    public Map<String, Object> uploadInternshipLog(String content,String name,HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Student student = (Student) session.getAttribute(STUDENT);
        //将内容保存到数据库
        InternshipLogger internshipLogger = new InternshipLogger();
        internshipLogger.setContent(content);
        internshipLogger.setId(UUID.randomUUID().toString());
        internshipLogger.setName(name);
        internshipLogger.setPath(LOG_PATH);
        internshipLogger.setStatus(NO_READ);
        internshipLogger.setTeacherName(student.getTeacherId());
        internshipLogger.setStudentNumber(student.getStuNo());
        internshipLogger.setUploadTime(new Date());
        boolean b = internshipLoggerService.addInternshipLogger(internshipLogger);
        if (b) {
            map.put(FLAG, true);
        }else {
            map.put(FLAG, false);
        }
        return map;
    }

    @GetMapping("/toShowInternshipLog.action")
    public String toShowInternshipLog() {
        return "student/showInternshipLog";
    }

    @RequestMapping("/getInternshipLogInfo.action")
    @ResponseBody
    public Map<String, Object> getInternshipLogInfo(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Student student = (Student) session.getAttribute("student");
        //根据学号查询日志
        List<InternshipLogger> internshipLoggers = internshipLoggerService.findInternshipLogByStudentNumber(student.getStuNo());
        System.out.println(internshipLoggers);
        map.put("data",internshipLoggers);
        map.put("count",internshipLoggers.size());
        map.put("code",0);
        map.put("msg","没有查询到实习日志");
        return map;
    }

    @RequestMapping("/updateInternshipLog.action")
    @ResponseBody
    public Map<String, Object> updateInternshipLog(InternshipLogger internshipLogger) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(internshipLogger);
        internshipLogger.setUploadTime(new Date());
        boolean b = internshipLoggerService.updateInternshipLogger(internshipLogger);
        if (b) {
            map.put("flag",true);
        }else {
            map.put("flag", false);
        }
        return map;
    }

    @RequestMapping("/deleteInternshipLog.action")
    @ResponseBody
    public Map<String, Object> deleteInternshipLog(String id) {
        Map<String, Object> map = new HashMap<>();
        boolean b = internshipLoggerService.deleteInternshipLog(id);
        if (b) {
            map.put("flag",true);
        }else {
            map.put("flag", false);
        }
        return map;
    }

    @RequestMapping("/getPersonInfo.action")
    @ResponseBody
    public Map<String, Object> getPersonInfo(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Student student = (Student) session.getAttribute("student");
        //System.out.println(academic);
        student = studentService.findStudentByStuNo(student.getStuNo());
        if (student == null) {
            return null;
        }
        map.put("student", student);
        return map;
    }

    @RequestMapping("/updatePerson.action")
    @ResponseBody
    public Map<String, Object> updatePerson(Student student) {
        Map<String, Object> map = new HashMap<>();
        boolean b = studentService.updateStudent(student);
        if (b) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }
        return map;
    }
}










